package android.medicalcloud.com.yiapp.yilian.ui;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.CommonController;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.PhoneTextIdentify;
import android.medicalcloud.com.yiapp.yilian.utils.android.ToastTool;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.Serializable;


/**
 * Created by wanggenlin on 17-5-18.
 */
public class FindPassActivity extends BaseActivity implements ViewNetCallBack{

    EditText userNameEt,mobileEt,veryEt,newPasEt,confirePasEt,paswEt;
    TextView smsSend,loginBt;
    int count = 120;
    private final int DISABLE_STAUES = 1;
    private final int ABLE_STAUES = 0;
    View paswLayout,userLayout,mobileLayout,veryLayout,confirePasLayout;


    private  void initMobileLayout(){

        paswLayout.setVisibility(View.GONE);
        confirePasLayout.setVisibility(View.GONE);
        userLayout.setVisibility(View.VISIBLE);
        mobileLayout.setVisibility(View.VISIBLE);
        veryLayout.setVisibility(View.VISIBLE);
        loginBt.setText("下一步");
        userNameEt.setEnabled(true);

    }

    private  void initCompleteLayout(){

        paswLayout.setVisibility(View.VISIBLE);
        userLayout.setVisibility(View.VISIBLE);
        mobileLayout.setVisibility(View.GONE);
        veryLayout.setVisibility(View.GONE);
        confirePasLayout.setVisibility(View.VISIBLE);
        loginBt.setText("完成");
        userNameEt.setEnabled(false);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (loginBt.getText().toString().contains("完成")){
                initMobileLayout();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }




    @Override
    protected void initView() {
        handler = new CountHandler();

        userNameEt = (EditText)findViewById(R.id.userNameEt);


        paswLayout = findViewById(R.id.paswLayout);
        userLayout = findViewById(R.id.userLayout);
        mobileLayout = findViewById(R.id.mobileLayout);
        veryLayout = findViewById(R.id.veryLayout);
        confirePasLayout = findViewById(R.id.confirePasLayout);



        mobileEt = (EditText)findViewById(R.id.mobileEt);
        veryEt = (EditText)findViewById(R.id.veryEt);
        newPasEt = (EditText)findViewById(R.id.newPasEt);
        paswEt = (EditText)findViewById(R.id.paswEt);

        confirePasEt = (EditText)findViewById(R.id.confirePasEt);
        smsSend = (TextView) findViewById(R.id.smsSend);
        loginBt = (TextView) findViewById(R.id.loginBt);
        findViewById(R.id.backIv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.loginBt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = veryEt.getText().toString();
                final String mobile = mobileEt.getText().toString();
                if (!loginBt.getText().toString().contains("完成")){
                    if (StringTools.isNullOrEmpty(code)) {
                        ToastTool.show("验证码不能为空");
                        return;
                    }
                    if (StringTools.isNullOrEmpty(mobile)) {
                        ToastTool.show("手机号码不能为空");
                        return;
                    }
                    if (!PhoneTextIdentify.isMobileNO(mobile)) {
                        ToastTool.show("手机号码格式不正确");
                        return;
                    }

                    CommonController.getInstance().verification(new ViewNetCallBack() {
                        @Override
                        public void onConnectStart(Object o) {

                        }

                        @Override
                        public void onConnectEnd() {

                        }

                        @Override
                        public void onFail(Exception e, Object o, String reslut) {

                        }

                        @Override
                        public void onData(Serializable result, int flag, boolean fromNet, Object o) {
                            Logger.e(o.toString()+"    reslut");
                            if (JsonTool.getString(o.toString(),"pass").equals("0")){
                                initCompleteLayout();
                            }
                        }
                    },mobile,code);
                    return;

                }

                final String name = userNameEt.getText().toString();
                final String pasw = paswEt.getText().toString();
                final String conPasw = confirePasEt.getText().toString();
                if (StringTools.isNullOrEmpty(name)) {
                    ToastTool.show("姓名不能为空");
                    return;
                }

                if (StringTools.isNullOrEmpty(pasw)) {
                    ToastTool.show("密码不能为空");
                    return;
                }

                if (StringTools.isNullOrEmpty(conPasw)) {
                    ToastTool.show("确认密码不能为空");
                    return;
                }
                if (!conPasw.equals(pasw)) {
                    ToastTool.show("密码和确认密码不一致");
                    return;
                }
                //String userName,String mobile,String verifyCode,String newPassword
                UserController.getInstance().findPasword(FindPassActivity.this,name,mobile,code,pasw);
            }
        });
        smsSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Logger.e("yanzhengmadianji");
                if (smsSend.isEnabled()) {
                    String mobile = mobileEt.getText().toString();
                    final String name = userNameEt.getText().toString();
                    if (StringTools.isNullOrEmpty(name)) {
                        ToastTool.show("姓名不能为空");
                        return;
                    }
                    if (StringTools.isNullOrEmpty(mobile)) {
                        ToastTool.show("手机号码不能为空");
                        return;
                    }
                    if (!PhoneTextIdentify.isMobileNO(mobile)) {
                        ToastTool.show("手机号码格式不正确");
                        return;
                    }


                    CommonController.getInstance().verify(FindPassActivity.this,mobile,name);
                    smsSend.setEnabled(false);

                }
            }
        });

        initMobileLayout();

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_findpassword;
    }

    CountHandler handler;

    private class CountHandler extends Handler {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);

            if (msg.what == DISABLE_STAUES) {

                smsSend.setText(  count + "s");
                count = count - 1;
//                smsSend.setTextColor(getResources().getColor(R.color.color_c2c2c2));
//                setBackgroundColor(getResources().getColor(R.color.selected_line_color));
                smsSend.setEnabled(false);

            }
            if (msg.what == ABLE_STAUES) {
                count = 60;
                smsSend.setText("发送验证码");
//                setBackgroundResource(R.drawable.common_btn_bg);
//                smsSend.setTextColor(getResources().getColor(R.color.color_66666));
                smsSend.setEnabled(true);

            }
        }
    }

    private class CountThread extends Thread {
        @Override
        public void run() {
            super.run();

            while (count > 0) {
                handler.sendEmptyMessage(DISABLE_STAUES);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    Logger.e(e.getLocalizedMessage(), e);
                }

            }

            handler.sendEmptyMessage(ABLE_STAUES);

        }
    }

    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {
        smsSend.setEnabled(true);
    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {

    }
    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        if (flag== HttpConfig.verify.getType()){
            Logger.e("----verify-----"+o.toString());
            if (JsonTool.getString(o.toString(),"status").equals("0")) {
             new CountThread().start();
             smsId = JsonTool.getString(o.toString(),"smsId");
             }
        }
        if (flag==HttpConfig.findPassword.getType()){
            if (JsonTool.getString(o.toString(),"status").equals("0")) {
                finish();
            }
        }
    }
    String smsId="";
}
